//
//  SearchViewController.m
//  LimitFree
//
//  Created by LZXuan on 15-6-9.
//  Copyright (c) 2015年 轩哥. All rights reserved.
//

#import "SearchViewController.h"
#import "JHRefresh.h"
#import "AppModel.h"

@interface SearchViewController ()

@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化处理(父类的不满足子类那么用重写设置)
    //把标题换了
    [self addTitleViewWithTitle:@"搜索结果"];
    //没有左右按钮
    self.navigationItem.leftBarButtonItem = nil;
    self.navigationItem.rightBarButtonItem = nil;
    
    //父类的tableView 多减了-49
    CGRect frame = self.tableView.frame;
    frame.size.height += 49;
    self.tableView.frame = frame;
    //调用 还要重写父类的
    [self firstDownload];
    [self creatRefreshView];
}
#pragma mark - 重写父类
- (void)firstDownload {
    self.currentPage = 1;
    NSString * url = [NSString stringWithFormat:self.url,self.currentPage,self.searchWord];
    //关键字有可能是汉字 是非法字符 转化为合法的
    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [self addTaskUrl:url isRefresh:YES];
}
- (void)addTaskUrl:(NSString *)url isRefresh:(BOOL)isRefresh {
    __weak typeof (self) weakSelf = self;
    //开始下载
    [_manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"下载成功");
        if (responseObject) {
            if (weakSelf.currentPage == 1) {
                [weakSelf.dataArr removeAllObjects];
            }
            
            //解析json
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            NSArray *arr = dict[@"applications"];
            //遍历数组解析
            for (NSDictionary *appDict in arr) {
                AppModel *model = [[AppModel alloc] init];
                //kvc 赋值
                [model setValuesForKeysWithDictionary:appDict];
                [weakSelf.dataArr addObject:model];
            }
            //刷新表格
            [weakSelf.tableView reloadData];
            [weakSelf endRefreshing];
            
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"下载失败");
        //结束刷新
        [weakSelf endRefreshing];
        
    }];

}
- (void)creatRefreshView {
    __weak typeof(self) weakSelf = self;
    
    [self.tableView addRefreshHeaderViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        if (weakSelf.isRefreshing) {
            return ;
        }
        weakSelf.currentPage = 1;
        weakSelf.isRefreshing = YES;
        
        NSString * url = [NSString stringWithFormat:weakSelf.url,weakSelf.currentPage,weakSelf.searchWord];
        //关键字有可能是汉字 是非法字符 转化为合法的
        url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [weakSelf addTaskUrl:url isRefresh:YES];
    }];
    
    [self.tableView addRefreshFooterViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        //上拉加载更多
        if (weakSelf.isLoadMoring) {
            return ;
        }
        weakSelf.isLoadMoring = YES;
        weakSelf.currentPage++;
        NSString * url = [NSString stringWithFormat:weakSelf.url,weakSelf.currentPage,weakSelf.searchWord];
        //关键字有可能是汉字 是非法字符 转化为合法的
        url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [weakSelf addTaskUrl:url isRefresh:YES];
    }];

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
